-
Notifications
You must be signed in to change notification settings - Fork 13.9k
In Option::get_or_insert_with(), forget the None instead of dropping it.
#148562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
|
cc @cyb0124 (this change is based on their idea) |
library/coretests/tests/option.rs
Outdated
| /// Test that `Option::get_or_insert_*` are usable in const contexts, including with types that | ||
| /// do not have `const Drop` (except for [`Option::get_or_insert()`], which is not const). | ||
| #[test] | ||
| fn option_const_get_or_insert() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe split this test such that we can use a more unambiguous name don't have to mention an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
library/core/src/option.rs
Outdated
| // It could also be expressed as `core::ptr::write(self, Some(f()))`; that would be | ||
| // additional unsafe code, so should be avoided unless a reason is found. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe include the unsafe block inside the code example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
…ing it. This allows eliminating the `T: [const] Destruct` bounds and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything. Ideally, the compiler would prove that that drop is not necessary itself, but it currently doesn't, even with `const_precise_live_drops` enabled.
Per #148486 (comment)
In
Option::get_or_insert_with(), after replacing theNonewithSome, forget theNoneinstead of dropping it.This allows eliminating the
T: [const] Destructbounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicitdrop_in_place::<Option<T>>()that will never do anything (and which might even persist after optimization).